package com.algorithmicTest1;

import java.util.ArrayList;

public class BasicSearch {
    public static void main(String[] args) {
        //实现基本查找：
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        //记录索引的位置
        int Index = 6;

        int temp = basicSearch1(arr,10);

        if (temp == -1){
            System.out.println("没有找到：");
            return;
        }
        System.out.println("找到了："+temp);
        boolean flag = basicSearch(arr,Index);
        if (!flag){
            System.out.println("没有找到：");
            return;
        }
        System.out.println("找到了");


        //思路2，用集合和数组来接受重复数字后返回的索引
        int newArr[] = {15,15,15,23,1,24,4,2,4,2,3,5,2,5,34,2};
        int[] newArr1 = basicSearch2(newArr,15);
        for (int i = 0; i < newArr1.length; i++) {
            System.out.println(newArr1[i]);
        }

        System.out.println("------------------------------------");
        ArrayList<Integer> list = basicSearch3(newArr,15);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }


    }
    //1：写一个方法来完成查询
    public static boolean basicSearch(int[] arr,int Index){
        for (int i = 0; i <arr.length; i++) {
            if(arr[i] == Index){
                return true;
            }
        }
        return false;
    }

    //2：写一个方法能够传回索引
    public static int basicSearch1(int[]arr, int Index) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == Index) {
                return i;
            }
        }
        return -1;
    }

    //3:利用数组用实现多个返回值
    public static int[] basicSearch2(int[] newArr, int Index) {
        int []temp  = new int[newArr.length];
        for (int i = 0; i < newArr.length; i++) {
            if (newArr[i] == Index) {
                temp[i] = i;
            }
        }
        return temp;
    }

    //4：利用集合来实现多个返回值
    public static ArrayList<Integer> basicSearch3(int[] arr, int Index) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] == Index){
                list.add(i);
            }
        }
        return list;
    }
}
